
import { _decorator, Component, Label, Node, find, EventTouch, Sprite } from 'cc';
import { App } from '../../App';
import CommConst from '../../const/CommConst';
import ScrollHelper from '../ScrollHelper';
import wsTool from '../../net/ws';
import { showTip } from '../../tools/PopuUp';
import UtilTool from '../../tools/UtilTool';
const { ccclass, property } = _decorator;

/* 走势界面 */
@ccclass('LBTrendPanel')
export class LBTrendPanel extends Component {
    @property(Node)
    contentNode: Node = null;

    @property(Node)
    scrollNode: Node = null;

    @property(Node)
    emptyNode: Node = null;

    private itemNum = 10
    // [listData]列表数据  [isLoading]是否加载中 [loadOver]是否加载结束(如果没有新的数据则认为是加载结束) [currPage]当前已经请求的第几页的数据 [dataDirty]是否需要重新拉取数据 
    private configData = {listData : [], isLoading : false, loadOver : false, currPage : 0, dataDirty : true, pageSize : 30}
    private parentObj = null
    protected onLoad(): void {
        
    }

    start() {
        let H = UtilTool.getGapSize().height*2
        if (H>0) {
            this.itemNum = this.itemNum + Math.ceil(H/280)
        }

        this.reqData()
    }

    setData(obj) {
        this.parentObj = obj

    }

    /**
     * 请求数据
     */
    reqData(pInit=true) {
        this.configData.isLoading = true
        
        wsTool.send({"msgCode":CommConst.Req_TREND})
        .then(res =>{
            // if (res?.result == 0) {
                // 数据回来
                this.configData.currPage = this.configData.currPage + 1
                this.configData.isLoading = false

                res.list.forEach(element => {
                    this.configData.listData.push(element)
                });
                if (res.list==null || res.list.length < this.configData.pageSize) {
                    this.configData.loadOver = true
                }
                this.emptyNode.active = this.configData.listData.length<=0?true:false
                // 最新请求的数据列表大于0，才会去刷新界面
                if (res.list.length>0) {
                    let param = {
                        init:pInit, 
                        data:this.configData.listData, 
                        itemNum:this.itemNum, 
                        itemFun: (obj,idx,objIdx)=>{
                            this.refreshItem(obj,idx,objIdx)
                        },
                        endFun:()=>{
                            this.OnScrollEnd()
                        }
                    }
    
                    this.scrollNode.getComponent(ScrollHelper).setData(param)
                }
                
            // }
            // else{
            //     alert('获取数据失败')
            // } 
        })
    }

    /** 
     * 刷新数据
     * param obj item对象
     * param idx 实际的下标索引
     * param objIdx item本身的下标索引
    */
    refreshItem(obj,idx,objIdx) {
        if (this.configData.listData.length <= 0) {
            return
        }

        let periodLabel = find("di/periodLabel", obj).getComponent(Label)   // 期号
        let ballSpr = find("di/ballSpr", obj).getComponent(Sprite)   // 球号码
        let hashLabel = find("di/hashLabel", obj).getComponent(Label) // 哈希值
        let btn_copy = find("di/btn_copy", obj) // 复制按钮

        // 期号、哈希值
        let itemData = this.configData.listData[idx]
        periodLabel.string = itemData.period
        hashLabel.string = UtilTool.getShortWalletAddr("0x" + itemData.hash)

        // 中奖球号
        ballSpr.spriteFrame = this.parentObj.ballArr[parseInt(itemData.nums)]
   
        btn_copy.off(Node.EventType.TOUCH_END)
        btn_copy.on(Node.EventType.TOUCH_END,(event: EventTouch) =>{
            App.Audio.playOneShot("click")
            UtilTool.copyText("0x" + itemData.hash,(resoult)=>{
                if (resoult){
                    showTip(App.language.getLanguageLab(161))
                }
            })
        },btn_copy)
        
    }

    // 滚动视图滑到底部回调
    OnScrollEnd() {
        if (this.configData.isLoading) {
            return
        }
        if (this.configData.loadOver) {
            return
        }

        // this.reqData(false)
    }

    /** 确定
     * @param 
      */
    clickSure() {
        App.Audio.playOneShot("click")
        this.node.destroy();
    }
}

